package com.ruoyi.eservice.service.impl;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.base.BaseService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.eservice.mapper.DishMapper;
import com.ruoyi.eservice.pojo.entity.Dish;
import com.ruoyi.eservice.service.IDishService;

/**
 * 菜品管理Service业务层处理
 *
 * @author Jackson
 * @date 2022-11-11
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper,Dish> implements IDishService
{
    @Autowired
    private DishMapper dishMapper;
    public <T> QueryWrapper<T> createSelWrapper(Class<T> type) {
        QueryWrapper<T> queryWrapper = Wrappers.query();
        queryWrapper.eq("del_flag", "0");
        return queryWrapper;
    }
    /**
     * 查询菜品管理
     *
     * @param dishId 菜品管理主键
     * @return 菜品管理
     */
    @Override
    public Dish selectDishByDishId(Long dishId)
    {
        return dishMapper.selectById(dishId);
    }

    /**
     * 查询菜品管理列表
     *
     * @param dish 菜品管理
     * @return 菜品管理
     */
    @Override
    public List<Dish> selectDishList(Dish dish)
    {
        QueryWrapper<Dish> queryWrapper = createSelWrapper(Dish.class);
        queryWrapper.lambda()
                .eq(StringUtils.isNotNull(dish.getDishId()), Dish::getDishId, dish.getDishId())
                .like(StringUtils.isNotEmpty(dish.getName()), Dish::getName, dish.getName())
                .eq(StringUtils.isNotNull(dish.getCategoryId()), Dish::getCategoryId, dish.getCategoryId())
                .eq(StringUtils.isNotNull(dish.getPrice()), Dish::getPrice, dish.getPrice())
                .eq(StringUtils.isNotNull(dish.getBusinessId()), Dish::getBusinessId, dish.getBusinessId())
                .eq(StringUtils.isNotEmpty(dish.getImage()), Dish::getImage, dish.getImage())
                .eq(StringUtils.isNotEmpty(dish.getDescription()), Dish::getDescription, dish.getDescription())
                .eq(StringUtils.isNotNull(dish.getStatus()), Dish::getStatus, dish.getStatus())
                .eq(StringUtils.isNotNull(dish.getSort()), Dish::getSort, dish.getSort())
                .eq(StringUtils.isNotNull(dish.getIsDeleted()), Dish::getIsDeleted, dish.getIsDeleted())
        ;
        return dishMapper.selectList(queryWrapper);
    }

    /**
     * 新增菜品管理
     *
     * @param dish 菜品管理
     * @return 结果
     */
    @Override
    public int insertDish(Dish dish)
    {
        dish.setCreateBy(SecurityUtils.getUsername());
        dish.setCreateTime(DateUtils.getNowDate());
        dish.setUpdateBy(SecurityUtils.getUsername());
        dish.setUpdateTime(DateUtils.getNowDate());
        int rows = dishMapper.insert(dish);
        return rows;
    }

    /**
     * 修改菜品管理
     *
     * @param dish 菜品管理
     * @return 结果
     */
    @Override
    public int updateDish(Dish dish)
    {
        dish.setUpdateBy(SecurityUtils.getUsername());
        dish.setUpdateTime(DateUtils.getNowDate());

        return dishMapper.updateById(dish);
    }

    /**
     * 批量删除菜品管理
     *
     * @param dishIds 需要删除的菜品管理主键
     * @return 结果
     */
    @Override
    public int deleteDishByDishIds(Long[] dishIds)
    {
        return dishMapper.deleteBatchIds(Arrays.asList(dishIds));
    }

    /**
     * 删除菜品管理信息
     *
     * @param dishId 菜品管理主键
     * @return 结果
     */
    @Override
    public int deleteDishByDishId(Long dishId)
    {
        return dishMapper.deleteById(dishId);
    }
}
